﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entities;
using System.IO;
namespace Stationery.UC
{
    public partial class UC_ReportBasket_Detail : System.Web.UI.UserControl
    {
        BLL_City bll_City = new BLL_City();
        BLL_Order bll_Order = new BLL_Order();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Session[Utils.SESSION_CART_ADMIN] != null)
                {
                    //strOrder_Code = Request.Params["order_code"].ToString();
                    //Session[Utils.SESSION_CART_ADMIN] = bll_Order.Lst_Order(bll_Order.Get_Order_OrderCode(strOrder_Code));

                    Set_DataSource_Grid();
                    Get_SourceCity();
                    Fill_Control();
                }
                else
                {
                    Response.Redirect(UtilsURL.admin);
                }
            }

        }
        private void Get_SourceCity()
        {
            ASPxComboBoxCity.DataSource = bll_City.Get_City_All();
            ASPxComboBoxCity.DataBind();
        }
        private void Set_DataSource_Grid()
        {
            if (Session[Utils.SESSION_CART_ADMIN] != null)
            {
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART_ADMIN];
                ASPxGridView1.DataSource = lstOrder;
                ASPxGridView1.DataBind();
                
            }
        }
        private void Fill_Control()
        {
            if (Request.Params["order_code"] != null)
            {
                string strOrder_Code = Request.Params["order_code"].ToString();
                List<Order> lstOrder = bll_Order.Lst_Order(bll_Order.Get_Order_OrderCode(strOrder_Code));
                ASPxTextBoxFullName.Text = lstOrder[0].FULLNAME;
                ASPxTextBoxAddress.Text = lstOrder[0].ADDRESS;
                ASPxComboBoxCity.Value = lstOrder[0].CITY;
                ASPxTextBoxPhone.Text = lstOrder[0].PHONE;
                ASPxTextBoxEmail.Text = lstOrder[0].EMAIL;
                ASPxDateEdit1.Date = lstOrder[0].RECEIVE_DATE;
                ASPxMemoNote.Text = lstOrder[0].DESCRIPTION;
            }
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            Int64 intID_PRODUCT = Convert.ToInt64(((LinkButton)sender).CommandArgument.ToString());
            if (Session[Utils.SESSION_CART_ADMIN] != null)
            {
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART_ADMIN];
                bll_Order.Delete_Order_FromList(lstOrder, intID_PRODUCT);
                Session[Utils.SESSION_CART_ADMIN] = lstOrder;
            }
            Response.Redirect(Request.RawUrl);
        }

        protected void ASPxButtonUpdate_Click(object sender, EventArgs e)
        {
            if (Session[Utils.SESSION_CART_ADMIN] != null)
            {
                if (((List<Order>)Session[Utils.SESSION_CART_ADMIN]).Count > 0)
                {
                    UpdateOrder();
                    Set_DataSource_Grid();
                }
            }
        }
        private void UpdateOrder()
        {
            List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART_ADMIN];
            for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
            {
                DevExpress.Web.ASPxEditors.ASPxSpinEdit sb = ASPxGridView1.FindRowCellTemplateControl(i, ASPxGridView1.Columns["QUANTITY"] as DevExpress.Web.ASPxGridView.GridViewDataColumn, "ASPxSpinEdit1") as DevExpress.Web.ASPxEditors.ASPxSpinEdit;
                Int64 intID_PRODUCT = Convert.ToInt64(ASPxGridView1.GetRowValues(i, "ID_PRODUCT"));
                Int64 intQUANTITY = Convert.ToInt64(sb.Value);
                for (int j = 0; j < lstOrder.Count; j++)
                {
                    if (intID_PRODUCT == lstOrder[j].ID_PRODUCT)
                    {
                        lstOrder[j].QUANTITY = intQUANTITY;
                        UpdatePriceValue(lstOrder[j]);
                        break;
                    }
                }
            }
            Session[Utils.SESSION_CART] = lstOrder;
        }
        private void UpdatePriceValue(Order pOrder)
        {
            pOrder.VALUE = pOrder.PRICE * pOrder.QUANTITY;
            pOrder.TOTAL = pOrder.VALUE + pOrder.VALUE * pOrder.TAX;
        }

        protected void ASPxButtonCancel_Click(object sender, EventArgs e)
        {
            if (Session[Utils.SESSION_CART_ADMIN] != null)
            {
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART_ADMIN];
                foreach (Order order in lstOrder)
                {
                    order.STATUS = Order.status_delete;
                    bll_Order.InsertUpdate_Order(order);
                }
                Response.Redirect(UtilsURL.neworder);
            }
        }

        protected void ASPxButtonSuccess_Click(object sender, EventArgs e)
        {
            if (Session[Utils.SESSION_CART_ADMIN] != null)
            {
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART_ADMIN];
                foreach (Order order in lstOrder)
                {
                    order.STATUS = Order.status_success;
                    bll_Order.InsertUpdate_Order(order);
                }
                Response.Redirect(UtilsURL.neworder);
            }
        }

        protected void ASPxButtonOrder_Click(object sender, EventArgs e)
        {
            if (Session[Utils.SESSION_CART_ADMIN] != null)
            {
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART_ADMIN];
                foreach (Order order in lstOrder)
                {
                    order.FULLNAME = ASPxTextBoxFullName.Text;
                    order.ADDRESS = ASPxTextBoxAddress.Text;
                    order.CITY = ASPxComboBoxCity.Text;
                    order.PHONE = ASPxTextBoxPhone.Text;
                    order.EMAIL = ASPxTextBoxEmail.Text;
                    order.DESCRIPTION = ASPxMemoNote.Text;
                    order.STATUS = "P";//pending
                    order.ORDER_DATE = DateTime.Now;
                    order.RECEIVE_DATE = ASPxDateEdit1.Date;
                    //order.ORDER_CODE = strOrderCode;
                    bll_Order.InsertUpdate_Order(order);
                }
                Response.Redirect(UtilsURL.neworder);
            }
        }

        protected void ASPxButtonExcel_Click(object sender, EventArgs e)
        {
            if (Session[Utils.SESSION_CART_ADMIN] != null)
            {
                BLL_Order bll_order = new BLL_Order();
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART_ADMIN];
                string strOrderCode = lstOrder[0].ORDER_CODE ;
                string strFileName = Server.MapPath(Utils.excel_directory_temp + DateTime.Now.ToString("ddMMyyyyhhmmss") + "_detail.xls");               
                //                
                MemoryStream ms = bll_order.ExportToExcel_NPOI(Server.MapPath(Utils.excel_directory), lstOrder, strFileName);                
                string saveAsFileName = string.Format("HoaDon-{0:d}-{1}-{2}.xls", lstOrder[0].RECEIVE_DATE.ToString("ddMMyyyy"), lstOrder[0].FULLNAME.Trim(), lstOrder[0].ADDRESS.Trim());


                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
                Response.Clear();
                Response.BinaryWrite(ms.ToArray());
                Response.End();
            }
        }
    }
}